package com.lfm.generic;

import java.util.ArrayList;
import java.util.Comparator;

@SuppressWarnings({"all"})
public class GenericExercise02 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("lfm", 250, new MyDate(2005,7,21)));
        employees.add(new Employee("lfm", 300, new MyDate(2005,6,21)));
        employees.add(new Employee("lfm", 350, new MyDate(2005,6,20)));

        System.out.println(employees);
        System.out.println("====排序后====");

        employees.sort(new Comparator<Employee>() {
           @Override
            public int compare(Employee e1, Employee e2) {
               int com =  e1.getName().compareTo(e2.getName());
               if (com != 0) {
                   return e1.getName().compareTo(e2.getName());
               }
               return e1.getBirthday().compareTo(e2.getBirthday());
           }
        });
        System.out.println(employees);
    }


}
class Employee {
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "\nEmployee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }


}
class MyDate implements Comparable<MyDate>{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }


    @Override
    public int compareTo(MyDate e) {
        int year = this.year - e.getYear();
        if (year != 0) {
            return year;
        }

        int month =  this.month - e.getMonth();

        if (month != 0) {
            return month;
        }

        return this.day - e.getDay();
    }
}

